<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/global.jsp" %>
<script type="text/javascript">

	$('#categoryId').combotree({
	    url : '${path }/category/allValid',
	    parentField : 'pid',
	    lines : true,
	    panelHeight : 'auto',
	    value : '${item.categoryId}'
	});

	
	   $('#itemEdit').form({
           url : '${path }/item/update',
           onSubmit : function() {
               progressLoad();
               var skusStr = skusJson();
               $('#skuList').val(skusStr);
               $('#categoryName').val($('#categoryId').combotree('getText'));
               var isValid = $(this).form('validate');
               if (!isValid) {
                   progressClose();
               }
               return isValid;
           },
           success : function(result) {
               progressClose();
               result = $.parseJSON(result);
               if (result.success) {
                   parent.$.modalDialog.openner_dataGrid.datagrid('reload');
                   //parent.layout_west_tree.tree('reload');
                   parent.$.modalDialog.handler.dialog('close');
               }
           }
       });
</script>

<div class="easyui-layout" style="width:800px;" data-options="fit:true,border:false">
	<div data-options="region:'north',border:true" >
	<form id="itemEdit" method="post">
	<div class="easyui-tabs">
	    <div title="基本信息" style="padding:10px">
			    <input type="hidden" name="categoryName" id="categoryName" />
				<input type="hidden" name="id" value="${item.id}" />
				<input type="hidden" name="skuList" id="skuList" />
			   	<table cellpadding="5">
					<tr>
						<td align="right">商品编码:</td>
						<td><input class="easyui-textbox" type="text" name="itemCode" id="itemCode" readonly="readonly" value="${item.itemCode}"></input></td>
						<td align="right">商品名称:</td>
						<td><input class="easyui-textbox" type="text" name="name" id="itemName" value="${item.name}"></input></td>
						<td align="right">英文名称:</td>
						<td><input class="easyui-textbox" type="text" name="enName" id="enName" value="${item.enName}"></input></td>
					</tr>
					<tr>
						<td align="right">商品简称:</td>
						<td><input class="easyui-textbox" type="text" name="itemShort" value="${item.itemShort}"></input></td>
						<td align="right">所属类目:</td>
						<td>
						 	<select id="categoryId" name="categoryId" style="width: 160px" ></select>
						</td>
						<td align="right">品牌:</td>
						<td>
						     <select id="brand" name="brand" style="width: 160px" class="easyui-combobox" >
							   <option value="" ></option>
								<c:forEach items="${brands }" var="brand">
									<option value="${brand.code }"   <c:if test="${brand.code==item.brand }">selected="selected"</c:if>>${brand.code } </option>
								</c:forEach>
							</select>
						</td>
						
					</tr>
					<tr>
						<td align="right">国家:</td>
						 <td>
							<select id="country" name="country" 
								class="easyui-combobox" style="width: 80px">
									<option value="0">无</option>
									<option value="1">美国</option>
									<option value="2">德国</option>
									<option value="3">日本</option>
									<option value="4">澳洲</option>
							</select>
						  </td>
						  <td align="right">币种:</td>
						   <td>
							<select id="currency" name="currency"
								class="easyui-combobox" style="width: 80px">
									<option value="1">人名币</option>
									<option value="2">美元</option>
							</select>
						  </td>
						  <td align="right">是否身份证:</td>
						   <td>
							<select id="idCard" name="idCard"
								class="easyui-combobox" style="width: 80px">
									<option value="1" >是</option>
									<option value="2">否</option>
							</select>
						  </td>
					
					</tr>
					<tr>
						<td align="right">销售开始时间:</td>
						<td><input  class="easyui-datebox"  type="text" name="startDate" id="startDate" value="${item.startDateStr}" ></input></td>
						<td align="right">销售结束时间:</td>
						<td><input  class="easyui-datebox"  type="text" name="endDate" id="endDate"  value="${item.endDateStr}" ></input></td>
						<td align="right">采购站点:</td>
						<td><input class="easyui-textbox" type="text" name="buySite" value="${item.buySite}"></input></td>
					</tr>
					<tr>
						<td align="right">规格:</td>
						<td><input class="easyui-textbox" type="text" name="spec" value="${item.spec}"></input></td>
						<td align="right">型号:</td>
						<td><input class="easyui-textbox" type="text" name="model" value="${item.model}"></input></td>
						<td align="right">重量:</td>
						<td><input class="easyui-numberbox" type="text" name="weight"  data-options="min:0,precision:2" value="${item.weight}"></input>KG</td>
					</tr>
						<tr>
						<td align="right">单位:</td>
						<td><input class="easyui-textbox" type="text" name="unit" value="${item.unit}"></input></td>
						<td align="right">来源:</td>
						<td><input class="easyui-textbox" type="text" name="source" value="${item.source}"></input></td>
						<td align="right">联系人:</td>
						<td><input class="easyui-textbox" type="text" name="contactPerson" value="${item.contactPerson}"></input></td>
					</tr>
						<tr>
						<td align="right">产地:</td>
						<td><input class="easyui-textbox" type="text" name="origin" value="${item.origin}" ></input></td>
						<td align="right">联系电话:</td>
						<td><input class="easyui-textbox" type="text" name="contactTel" value="${item.contactTel}"></input></td>
						<td align="right">备注:</td>
						<td></td>
					</tr>
				</table>
			</div>
			<div title="上传图片" style="padding:10px">
				<input id="fileupload" type="file" name="files[]" data-url="server/php/" multiple>
			</div>
			<div title="商品详情" style="padding:10px">
				备注:
				<input class="easyui-textbox" type="text" name="remark" value="${item.remark}"></input>
			</div>
		</div>
	</form>
	</div>
	<div data-options="region:'center'">
	   <div id="tb">
			<a href="javascript:append()" class="easyui-linkbutton"
				data-options="iconCls:'icon-add',plain:true" id="addButton">新增SKU</a> <a
				href="javascript:remove()" class="easyui-linkbutton"
				data-options="iconCls:'icon-remove',plain:true" id="delButton">删除SKU</a>
				<a href="javascript:testjson1()" class="easyui-linkbutton"
				data-options="iconCls:'icon-remove',plain:true" id="delButton">test</a>
		</div>
		<table id="itemEditGrid" data-options="border:false,fit:true ,iconCls: 'icon-edit',
				singleSelect: true
				"  class="easyui-datagrid" rownumbers="true"  singleSelect="true">
		<thead data-options="frozen:true">
			<tr>
				<th data-options="field:'id',width:40,hidden:true">ID</th>
			</tr>
		</thead>
		 	<thead>
				<tr>
				    <th data-options="field:'skuCode',width:80">SKU编码</th>
					<th data-options="field:'color',width:80,editor:'text'">颜色</th>
					<th data-options="field:'scale',width:80,editor:'text'">尺寸</th>
					<!-- <th data-options="field:'freight',width:80">运费</th> -->
					<th data-options="field:'inventory',width:80,editor:'numberbox'">库存</th>
					<th data-options="field:'virtualInventory',width:80,editor:'numberbox'">虚拟库存</th>
					<!-- <th data-options="field:'skuCode',width:80">SKU编码</th> -->
					<th data-options="field:'barCode',width:80,editor:'text'">barcode</th>
					<th data-options="field:'weight',width:80,editor:{type:'numberbox',options:{precision:2}}">重量(KG)</th>
					
				</tr>
				
    		</thead>
    		  <tbody>
    		   <c:forEach var="mysku" items="${item.itemSkus}">
				<tr>
				    <td>${mysku.id }</td>
				    <td>${mysku.skuCode }</td>
					<td>${mysku.color }</td>
					<td>${mysku.scale }</td>
					<td>${mysku.inventory }</td>
					<td>${mysku.virtualInventory }</td>
					<td>${mysku.barCode }</td>
					<td>${mysku.weight }</td>
				</tr>
				</tbody>
				</c:forEach>
		</table>
	</div>
</div>
<script type="text/javascript">
	
	function append() {
		/* if (endEditing()) {
			$('#dg').datagrid('appendRow', {});
			editIndex = $('#dg').datagrid('getRows').length - 1;
			$('#dg').datagrid('selectRow', editIndex).datagrid('beginEdit',
					editIndex);
		} */

		$('#itemEditGrid').datagrid('appendRow', {
			
		});
	}

	//删除一行
	function remove() {
		if(myLastIndex==-1)
			return;
		$('#itemEditGrid').datagrid('deleteRow', myLastIndex);
		myLastIndex = -1;
	}
	
	function isNull(data){ 
		return (data == "" || data == undefined || data == null); 
	}

	function skusJson() {
		$('#itemEditGrid').datagrid('endEdit', myLastIndex);
		var rows = $("#itemEditGrid").datagrid("getRows");
		if(rows.length>0){
		var arrayObj = new Array([rows.length]);
		 for (var i = 0; i < rows.length; i++) {
			//alert(rows[i].color); //获取指定列 
			 var sku = new Object(); 
		    if(!isNull(rows[i].id)){
			 sku.id = rows[i].id; 
			}
			if(!isNull(rows[i].color)){
			 sku.color = rows[i].color; 
			}
			if(!isNull(rows[i].scale)){
			 sku.scale = rows[i].scale;
			}
			if(!isNull(rows[i].inventory)){
			 sku.inventory = rows[i].inventory;
			}
			if(!isNull(rows[i].virtualInventory)){
			 sku.virtualInventory = rows[i].virtualInventory;
			}
			if(!isNull(rows[i].weight)){
			 sku.weight = rows[i].weight;
			}
		    if(!isNull(rows[i].skuCode)){
			 sku.skuCode = rows[i].skuCode;
			}
			 arrayObj[i] = sku;
		} 
		return JSON.stringify(arrayObj);
		}
	}
	var myLastIndex=-1;
	function setLastIndex(i) {
		myLastIndex = i;
	}
	
	$(function() {
		var lastIndex;
		$('#itemEditGrid').datagrid({
			onClickRow : function(rowIndex) {
				//if (lastIndex != rowIndex){
				$(this).datagrid('endEdit', lastIndex);
				$(this).datagrid('beginEdit', rowIndex);
				//}
				lastIndex = rowIndex;
				setLastIndex(rowIndex);
			},
		});
		
		
	});
	
	//国家
	var selectCountry="${item.country}";
	//币种
	var selectCurrency="${item.currency}";
	//是否身份证
	var selectIdCard="${item.idCard}";
	
	$(document).ready(function(){ 
		$('#country').val(selectCountry);
		$('#currency').val(selectCurrency);
		$('#idCard').val(selectIdCard);
	}); 
	
	
</script>
